const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const ZXUser = db.collection('ZX-User')
const ZXUserAccount = db.collection('ZX-User-Account')
const ZXUserReal = db.collection('ZX-User-Real')
const nowDate = db.serverDate()
const nowDateNew = new Date().getTime()

// 获取openId云函数入口函数
exports.main = async (event, context,token) => {

    var params = {}
    var api = event.api
    var _id = event.data._id
    var gameName = event.data.gameName
    var gameCode = event.data.gameCode
    var roleInfo = event.data.roleInfo
    var status = event.data.status

    if(api=='api-user-list'){
        if(gameName)
            params.gameName = gameName
        if(gameCode)
            params.gameCode = gameCode
        const res = await ZXUser.where(params).get()
        return res
    }

    if(api=='api-user-update'){
        if(roleInfo)
            params['powerInfo.roleInfo'] = roleInfo
        if(status)
            params.status = status
        
        if(_id){
            const res = await ZXUser.doc(_id).update(params)
            return res
        }else{
            return {
                type:'error',
                msg: '不能对id为空的数据进行修改！'
            }
        }
    }

    if(api=='api-user-detail'){
        const res = await ZXUser.doc(token).get()
        return res.data[0]
    }


    if(api=='api-user-edit'){
        delete event.data._id
        if(_id){
            const res = await ZXUser.doc(_id).update(event.data)
            return res
        }
    }


    // 预账号

    // 获取详情
    if(api=='api-user-real-detail'){
        if(token){
            const res = await ZXUserReal.doc(token).get()
            return res
        }
    }

    // 提交审核
    if(api=='api-user-real-add'){
        var gameCode = event.data.gameCode
        event.data.createTime = nowDate;
        if(gameCode)
            params.gameCode = gameCode

        // const res = await ZXUserReal.where(params).get()
        // if(res.data&&res.data.length>0){
        //     return {
        //         type: 'error',
        //         msg: '请勿重复提交',
        //     }
        // }else{
        //     event.data.status = 0
        //     return await ZXUserReal.add(event.data)
        // }

        // 1.验证是否账号和密码是否正确
        const resCode = await ZXUserReal.where(params).get()

        // 2.验证状态是否正常
        const resStatus = await ZXUserReal.where({
            gameCode: gameCode,
            status: _.neq(1),
        }).get()
        // return resStatus
        if(resStatus.data&&resStatus.data.length>0){
            return {
                type: 'error',
                msg: '此账号审核失败，请联系管理员！'
            }
        }else{
            if(resCode.data&&resCode.data.length>0){
                return {
                    type: 'error',
                    msg: '请勿重复提交'
                }
            }else{
                event.data.status = 0
                return await ZXUserReal.add(event.data)
            }
        }

        
    }

    // 获取预账号列表
    if(api=='api-user-real-list'){
        if(gameName)
            params.gameName = gameName
        if(gameCode)
            params.gameCode = gameCode

        const res = await db
        .collection('ZX-User-Real')
        .aggregate()
        .match(params)
        .sort({
            createTime: -1,
        })
        .skip((event.data.pageIndex - 1) * event.data.pageSize)
        .limit(event.data.pageSize)
        .end()
        const list = res.data
        const total = (await ZXUserReal.where(params).count()).total
    
        return {
            list: list,
            total: total,
        }

    }


    if(api=='api-user-real-update'){
        var status = event.data.status
        if(status)
            params.status = status

        return ZXUserReal.doc(_id).update({
            status: status,
        })
    }


    // 账号信息相关接口
    if(api=='api-user-account-add'){
        // 开启和关闭数据提交
        let isOpen = true
        if(isOpen){
            // 判断是否已经存在
            const total = (await ZXUserAccount.where({
                cUserId: token,
            }).count()).total

            if(total){
                let _id = event.data._id
                event.data.cUpdateTime = nowDateNew
                delete event.data._id
                return await ZXUserAccount.doc(_id).update(event.data)
            }else{
                event.data.cUserId = token
                event.data.cCreatTime = nowDateNew
                event.data.cUpdateTime = nowDateNew
                return await ZXUserAccount.add(event.data)
            }
        }else{
            return {
                type: "error",
                msg: "管理员没有开启提交功能！",
            }
        }

    }

    if(api=='api-user-account-detail'){
        const res =  await ZXUserAccount.where({
            cUserId: token,
        }).get()

        return res.data[0]
    }

    if(api=='api-user-account-list'){
        const res =  await ZXUserAccount.get()
        return res.data
    }


};